﻿@using viBook.Models
@model IEnumerable<User>
@{
    ViewBag.Title = "Users";
    ViewBag.Menu = "Management";
    Layout = "~/Views/Shared/_AdminLayout.cshtml";
}
@(Html.Telerik().Grid(Model)
               .Name("user-data-grid")
               .DataKeys(keys => keys.Add(user => user.Id))
               .Columns(columns =>
                            {
                                columns.Bound(user => user.Id)
                                    .Width(50)
                                    .Title("ID");
                                columns.Bound(user => user.DisplayName)
                                    .Template(
                                        @<text>
                                        @Html.ActionLink(item.DisplayName, "Index", "Account", new { id = item.Id }, null)
                                        </text>
                                    )
                                    .ClientTemplate(string.Format("<a href='/Account/Details/<#= Id #>'><#= DisplayName #></a>"))
                                    .Width(200)
                                    .Title("Display name");
                                columns.Bound(user => user.Username)
                                    .Title("Username");
                                columns.Bound(user => user.Email);
                                columns.Bound(user => user.IsBlocked).Title("Is Blocked").Width(80);
                                columns.Bound(user => user.Id)
                                    .Template(
                                        @<text> 
                                            <a class='t-button' href='javascript:blockUser(@item.Id)'>Block</a>
                                            <a class='t-button' href='javascript:unBlockUser(@item.Id)'>Unblock</a>
                                        </text>)
                                    .ClientTemplate(string.Format("<a class='t-button' href='javascript:blockUser(<#= Id #>)'>Block</a><a class='t-button' href='javascript:unBlockUser(<#= Id #>)'>Unblock</a>"))
                                    .Title(string.Empty)
                                    .Width(190);
                                columns.Command(c => c.Delete());
                            })
                .DetailView(details =>
                                   {
                                       details.Template(i => @Html.Partial("_UserDetailServerTemplate", i).ToHtmlString());
                                       details.ClientTemplate(@Html.Partial("_UserDetailClientTemplate").ToHtmlString());
                                   })
               .DataBinding(dataBinding =>
                                {
                                    dataBinding.Server().Select("Users", "Admins");
                                    dataBinding.Ajax().Select("SelectUserAjax", "Admins").Enabled(true);
                                    dataBinding.Ajax().Delete("DeleteUserAjax", "Admins").Enabled(true);
                                })
               .Editable(editing => editing.Mode(GridEditMode.InLine))
               .Scrollable()
               .Sortable()
               .Pageable()
               .Footer(true)
        )
<script type="text/javascript">
    function blockUser(id) {
        $.ajax({
            url: '@Url.Action("BlockUser", "Admins")',
            type: 'POST',
            data: "{ id:" + id + "}",
            contentType: 'APPLICATION/JSON',
            success: function () {
                var userGrid = $('#user-data-grid').data('tGrid');
                userGrid.rebind();
            }
        });
    }
    function unBlockUser(id) {
        $.ajax({
            url: '@Url.Action("UnblockUser", "Admins")',
            type: 'POST',
            data: "{ id:" + id + "}",
            contentType: 'APPLICATION/JSON',
            success: function () {
                var userGrid = $('#user-data-grid').data('tGrid');
                userGrid.rebind();
            }
        });
    }
</script>
